package com.me.GameWorld;

import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.me.GameObjects.Bubble;
import com.me.GameObjects.Grid;

public class GameWorld {

	private Grid grid;
	private ArrayList<Bubble> bubbles;
	private Bubble newBubble;
	private GameRenderer renderer;
	private float runTime = 0;
	
	private Rectangle mesa ;
	
	public GameWorld() {
		// TODO Auto-generated constructor stub
		int x =480;
		int y = 800;
		grid =  new Grid(0,0,x, y);
		createGrid(grid.getWidth(), grid.getHeight(), grid.getPosition(), 50);
		mesa = new Rectangle(grid.getPosition().x, grid.getPosition().y, grid.getWidth()+10, grid.getHeight()+20);
			
	}
	
	public ArrayList<Bubble> getBubbles() {
		return bubbles;
	}

	public void setBubbles(ArrayList<Bubble> bubbles) {
		this.bubbles = bubbles;
	}

	public void update(float delta) {
		// TODO Cambiar el rectangulo por el grid del la bolsa de burbujas
		
		runTime += delta;
		
		
	}
	
	
	/*
	 * crea la malla de burbujas por defecto
	 */
	public void createGrid(int width,int height,Vector2 position,float radious){    	
		float ancho=width+position.x;
		float alto=height+position.y;    	
		bubbles=new ArrayList<Bubble>();    	
		float j= position.y+radious;
		float diameter=radious*2;
		boolean change=true;
		for (float i = position.x+radious; i <= ancho; i=i+diameter) {
			if(j>=alto){
				break;
			}else{
				Bubble b= new Bubble(i,j,radious);
				//System.out.println("@@@@@@@@@@@pos"+i+"#"+j);
				bubbles.add(b);
			}        	
			if(i+diameter>=ancho && change){
				i=position.x;
				j=j+diameter;
				change=false;
			}
			else if(i+diameter>=ancho && !change){
				i=position.x-radious;
				j=j+diameter;     
				change=true;
			}
		}
	}

	/*
	 *cambia una burbuja en la malla, dada su posicion y su nuevo estado de burbuja
	 */    
	public void changeGrid(int pos,Bubble bu){
		Bubble b=bubbles.get(pos);
		newBubble=b;
		b=bu;
	}

	
	public Bubble getNewBubble() {
		return newBubble;
	}

	public void setNewBubble(Bubble newBubble) {
		this.newBubble = newBubble;
	}

	public void cambiarEstado(float px,float py,Color estado){
		
		ArrayList<Bubble> list=bubbles;
    	for (int i = 0; i <list.size(); i++) {
			Bubble b=list.get(i);
			if(b.getBubble().contains(px, py)){
				if (b.getEstado()!=Bubble.EXPLOTADO){
				b.setEstado(estado);
				changeGrid(i, b);
				}
				break;
			}
		}
		
	}


	
	public Rectangle getMesa(){
		return mesa;
	}
	
	public Grid getGrid(){
		return grid;
	}
}
